package com.snake.mylibrary.loader;


import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.content.Loader;

import com.snake.mylibrary.utils.LogUtil;
import com.snake.mylibrary.utils.ThreadManagerUtil;

public abstract class DynamicLoader extends Loader<DynamicResult> {
	private boolean isLoading;

	public DynamicLoader(Context context) {
		super(context);
	}

	@Override
	protected void onForceLoad() {

		new WorkTask().executeOnExecutor(ThreadManagerUtil.getInstance().getFixedThreadPool());
	}
	
	@Override
	protected void onStartLoading() {
		if(isLoading){
			LogUtil.e("取消一个加载");
			return;
		}
		forceLoad();
		LogUtil.e("onStartLoading；　"+isLoading);
	}
	

	protected abstract DynamicResult createDynamicResult();
	
	class WorkTask extends AsyncTask<Void, Integer, DynamicResult>
	{
		@Override
		protected void onPreExecute() {
			isLoading = true;
		}

		@Override
		protected DynamicResult doInBackground(Void... params) {
			DynamicResult ret = createDynamicResult();
			return ret;
		}

		@Override
		protected void onPostExecute(DynamicResult result) {
			deliverResult(result);
			isLoading = false;
			LogUtil.e("isLoading: "+isLoading);
		}
	}
	
	@Override
	public void deliverResult(DynamicResult data) {
		super.deliverResult(data);

	}

}
